package com.android.test.util;

import java.util.ArrayList;

import com.android.test.model.TestRecord;
import com.android.test.provider.TestResultProvider;
import com.android.test.provider.TestSheet;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class DataRecordUtil {
    // For DB query main table use
    public static final int SUBJECT_COLUMN = 1;
    public static final int GRADE_COLUMN = 2;
    public static final int WEEK_COLUMN = 3;
    public static final int SCORE_COLUMN = 4;
    public static final int DATE_COLUMN = 5;
    public static final int DURATION_COLUMN = 6;
    public static final int NOTE_COLUMN = 7;

    private static String[] mDataColumns = { TestSheet.SUBJECT,
	    TestSheet.GRADE, TestSheet.WEEK, TestSheet.SCORE, TestSheet.DATE,
	    TestSheet.DURATION };

    public static void addTestResult(Context context, TestRecord record) {
	ContentResolver cr = context.getContentResolver();

	ContentValues values = new ContentValues();

	values.put(TestResultProvider.KEY_SUBJECT, record.getSubject());
	values.put(TestResultProvider.KEY_GRADE, record.getGrade());
	values.put(TestResultProvider.KEY_WEEK, record.getWeek());
	values.put(TestResultProvider.KEY_SCORE, record.getScore());
	values.put(TestResultProvider.KEY_DATE,
		DateUtil.getDate(record.getDate()));
	values.put(TestResultProvider.KEY_DURATION, record.getTestTime());
	values.put(TestResultProvider.KEY_NOTE, record.getNote());
	deleteTestResult(context, record);
	cr.insert(TestResultProvider.CONTENT_URI, values);

    }

    public static boolean deleteTestResult(Context context, TestRecord record) {
	if (checkResultExist(context, record)) {
	    deleteTestResultImpl(context, record);
	    return true;
	} else {
	    return false;
	}
    }

    private static void deleteTestResultImpl(Context context, TestRecord record) {
	ContentResolver cr = context.getContentResolver();

	String mSubjectFilter = SQLUtil.textFilterBuilder(
		TestResultProvider.KEY_SUBJECT, record.getSubject());
	String mGradeFilter = SQLUtil.integerFilterBuilder(
		TestResultProvider.KEY_GRADE, record.getGrade());
	String mWeekFilter = SQLUtil.integerFilterBuilder(
		TestResultProvider.KEY_WEEK, record.getWeek());
	String mQuery = SQLUtil.and(mSubjectFilter, mGradeFilter, mWeekFilter);
	Log.e("zzy", " = " + mQuery);
	// mQuery = SQLUtil.and(mQuery, mWeekFilter);
	cr.delete(TestResultProvider.CONTENT_URI, mQuery, null);
    }

    private static boolean checkResultExist(Context context, TestRecord record) {
	ContentResolver cr = context.getContentResolver();

	String mSubjectFilter = SQLUtil.textFilterBuilder(
		TestResultProvider.KEY_SUBJECT, record.getSubject());
	String mGradeFilter = SQLUtil.integerFilterBuilder(
		TestResultProvider.KEY_GRADE, record.getGrade());
	String mWeekFilter = SQLUtil.integerFilterBuilder(
		TestResultProvider.KEY_WEEK, record.getWeek());

	String mQuery = SQLUtil.and(mGradeFilter, mSubjectFilter, mWeekFilter);
	Log.e("zzy", "check = " + mQuery);
	// mQuery = SQLUtil.and(mQuery, mWeekFilter);

	if (cr.query(TestResultProvider.CONTENT_URI, null, mQuery, null, null)
		.getCount() == 0) {
	    return false;
	} else {
	    return true;
	}
    }

    public static ArrayList<TestRecord> getTestResultBySubjectAndGrade(
	    Context context, String requestSubject, int requestGrade,
	    String order) {
	ArrayList<TestRecord> testReport = new ArrayList<TestRecord>();
	ContentResolver cr = context.getContentResolver();

	String mSubjectFilter = SQLUtil.textFilterBuilder(
		TestResultProvider.KEY_SUBJECT, requestSubject);
	Cursor c;
	try {
	    c = cr.query(TestResultProvider.CONTENT_URI, null, mSubjectFilter,
		    null, order);
	} catch (RuntimeException e) {
	    c = null;
	}
	if (c != null && c.moveToFirst()) {
	    do {
		int grade = c.getInt(GRADE_COLUMN);
		int week = c.getInt(WEEK_COLUMN);
		int score = c.getInt(SCORE_COLUMN);
		String date = c.getString(DATE_COLUMN);
		long duration = c.getLong(DURATION_COLUMN);
		String note = c.getString(NOTE_COLUMN);

		if (requestGrade == -1) {
		    TestRecord record = new TestRecord(requestSubject, grade,
			    week, score, DateUtil.parseDate(date), duration,
			    note);
		    testReport.add(record);
		} else {
		    if (requestGrade == grade) {
			TestRecord record = new TestRecord(requestSubject,
				grade, week, score, DateUtil.parseDate(date),
				duration, note);
			testReport.add(record);
		    }
		}
	    } while (c.moveToNext());
	    c.close();
	    return testReport;
	}
	return null;
    }
}
